package com.syezon.wifikey.wifi;

import android.arch.lifecycle.MutableLiveData;

import com.syezon.wifikey.app.WiFiKeyApp;
import com.syezon.wifikey.db.BlackWifiDao;
import com.syezon.wifikey.wifi.info.BlackWifiInfo;

import java.util.List;

/**
 * Created by Administrator on 2018/9/14.
 */

public class BlackWifiService {
    private static BlackWifiService blackWifiService;

    private MutableLiveData<List<BlackWifiInfo>> blackWifiLiveData = new MutableLiveData<>();
    private BlackWifiDao blackWifiDao;

    private BlackWifiService() {
        blackWifiDao = WiFiKeyApp.getDatabase().getBlackDao();
    }

    public static BlackWifiService getInstance() {
        if (blackWifiService == null) {
            synchronized (BlackWifiService.class) {
                if (blackWifiService == null) {
                    blackWifiService = new BlackWifiService();
                }
            }
        }
        return blackWifiService;
    }

    public MutableLiveData<List<BlackWifiInfo>> getBlackWifiLiveData() {
        return blackWifiLiveData;
    }

    /**
     * 获取wifi黑名单列表
     */
    public void getBlackWifiList() {
        List<BlackWifiInfo> all = blackWifiDao.getAll();
        blackWifiLiveData.postValue(all);
    }

    public void addBlackWifi(BlackWifiInfo blackWifiInfo) {
        blackWifiDao.insertAll(blackWifiInfo);
        getBlackWifiList();
        WifiListUtils.getInstance().updateWifiList();
    }

    public void deleteBlackWifi(BlackWifiInfo blackWifiInfo) {
        blackWifiDao.delete(blackWifiInfo);
        getBlackWifiList();
        WifiListUtils.getInstance().updateWifiList();
    }

    public boolean isBlackWifi(String ssid) {
        boolean isBlackWifi = false;
        List<BlackWifiInfo> all = blackWifiDao.getAll();
        for (int i = 0; all != null && i < all.size(); i++) {
            BlackWifiInfo blackWifiInfo = all.get(i);
            if (ssid.equals(blackWifiInfo.getSsid())) {
                isBlackWifi = true;
                break;
            }
        }
        return isBlackWifi;
    }
}
